package yxy.game.pm2.bean.area;

import org.redisson.api.RMap;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.module.BaseGameRedisBean;

import java.util.ArrayList;
import java.util.Collection;

//跨服战区，跨服战分组；同组里进行跨服战
public class AreaGroupBean extends BaseGameRedisBean {
	// bean===========================================================================

	private int groupId;
	private ArrayList<String> areaList = new ArrayList<>();
	private int clusterId;

	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	public ArrayList<String> getAreaList() {
		return areaList;
	}

	public void setAreaList(ArrayList<String> areaList) {
		this.areaList = areaList;
	}

	public int getClusterId() {
		return clusterId;
	}

	public void setClusterId(int clusterId) {
		this.clusterId = clusterId;
	}

	// func==========================================================================

	public void addArea(int areaId) {
		try {
			if (tryLock()) {
				AreaBean areaBean = AreaBean.get(areaId);
				if (areaBean == null) {
					return;
				}
				int oldgroupId = areaBean.getGroupId();
				if (oldgroupId == getGroupId()) {
					return;
				}
				AreaGroupBean oldGroup = AreaGroupBean.get(oldgroupId);
				if (oldGroup != null) {
					oldGroup.removeArea(areaId);
				}
				areaBean.setGroupId(getGroupId());
				areaBean.set();
				String areaCode = String.valueOf(areaId);
				if (getAreaList().contains(areaCode)) {
					return;
				}
				getAreaList().add(areaCode);
				set();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			unlock();
		}
	}

	public void removeArea(int areaId) {
		try {
			if (tryLock()) {
				AreaBean areaBean = AreaBean.get(areaId);
				if (areaBean == null) {
					return;
				}
				int oldgroupId = areaBean.getGroupId();
				if (oldgroupId == getGroupId()) {
					areaBean.setGroupId(0);
					areaBean.set();
				}
				String areaCode = String.valueOf(areaId);
				if (!getAreaList().contains(areaCode)) {
					return;
				}
				getAreaList().remove(areaCode);
				set();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			unlock();
		}
	}

	// redis==========================================================================

	static public String key() {
		return String.format("AREA:GROUP:MAP");
	}

	static private String groupKey(int groupId) {
		return String.format("group_%s", groupId);
	}

	static private RMap<String, AreaGroupBean> touch() throws RedisException {
		RedisConnection redis = RedisManager.getInstance().getConnection(RedisType.REDIS_GAME);
		return RedisManager.client(redis).getMap(key());
	}

	static public Collection<AreaGroupBean> all() {
		try {
			return touch().readAllValues();
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return new ArrayList<>();
	}

	static public AreaGroupBean get(int groupId) {
		try {
			RMap<String, AreaGroupBean> map = touch();
			AreaGroupBean area = map.get(groupKey(groupId));
			return area;
		} catch (RedisException e) {
			e.printStackTrace();
			return null;
		}
	}

	/** 生成一个新战区 */
	static public AreaGroupBean create(int clusterId, int groupId) throws RedisException {
		AreaGroupBean groupBean = new AreaGroupBean();
		groupBean.setClusterId(clusterId);
		groupBean.setGroupId(groupId);
		groupBean.set();
		return groupBean;
	}

	public void set() {
		try {
			RMap<String, AreaGroupBean> map = touch();
			map.put(groupKey(getGroupId()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String lockKey() {
		return String.format("lock:%s", key());
	}

}
